/*
 * @Author: Xavier 1305433186@qq.com
 * @Date: 2024-07-27 16:15:43
 * @LastEditors: Xavier 1305433186@qq.com
 * @LastEditTime: 2024-08-16 18:10:17
 * @FilePath: \JS32T031_SDK\User\mini_scheduler_V2\tasks.c
 * @Description:
 *
 * STAYTRUE (c) 2024 by ${Xavier}, All Rights Reserved.
 */

#include "tasks.h"
#include "main.h"
#include "system_task.h"
#include "board.h"
#include "driver_board_protocol_analysis.h"
#include "433_decoding/433_software_decoding.h"

int Running = 1;

CSystemTask systemtask;
CShowTask show;
CDriverBoardTask driverboardtask;
CKeyTask key;
TaskFun(SystemTask)
{
  _SS while (Running)
  {
    //  printf("SystemTask ");
    uint8_t send_buffer[6];
    send_buffer[0] = 0xFE;
    send_buffer[1] = 0xF1;
    send_buffer[2] = 0x04;
    send_buffer[3] = 0x00;
    send_buffer[4] = 0xF5;
    send_buffer[5] = 0xFF;
    driver_board_uart_send(send_buffer, 6);
    WaitX(3); // 100ms
  }
  _EE
}
TaskFun(ShowTask)
{
  _SS while (Running)
  {
    static unsigned int time_count;
    updata_show_dist(time_count++);
    // updata_show_cal(567);
    WaitX(1); // 100ms
  }
  _EE
}
TaskFun(DriverBoardTask)
{
  _SS while (Running)
  {
    unsigned char str[WIRELESS_433_BYTE];
    unsigned char count;
    count = get_433_key_stroke(str);
    if (count != 0)
    {
      printf("%u", count);
      printf(" %X", str[0]);
      printf(" %X", str[1]);
      printf(" %X", str[2]);
    }
    driver_board_task();
    WaitX(2); // 100ms
  }
  _EE
}
TaskFun(KeyTask)
{
  _SS while (Running)
  {
    WaitX(1); // 100ms
  }
  _EE
}
void UpdateTimers(void)
{
  UpdateTimer(systemtask);
  UpdateTimer(show);
  UpdateTimer(driverboardtask);
  // UpdateTimer(key);
}

void runtasks(void)
{
  RunTask(SystemTask, systemtask);
  RunTask(ShowTask, show);
  RunTask(DriverBoardTask, driverboardtask);
  // RunTask(KeyTask, key);
}
